package com.example.hello.views;

import com.example.hello.*;
import com.example.hello.views.badges.Badges;
import com.example.hello.views.organizations.Organizations;
import com.example.hello.views.submissions.Submissions;
import com.example.hello.views.tracks.Tracks;
import com.example.hello.views.users.Users;
import com.vaadin.server.FileResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;

import java.io.File;

public class StaticView extends VerticalLayout {

    BootstrapNavbar navbar = new BootstrapNavbar();
    HorizontalLayout header = new HorizontalLayout();
    HorizontalLayout footer = new HorizontalLayout();

    BootstrapButtonGroup mainButtonGroup = new BootstrapButtonGroup();
    BootstrapButton organizations = new BootstrapButton("Organizations");
    BootstrapButton tracks = new BootstrapButton("Tracks");
    BootstrapButton submissions = new BootstrapButton("Submissions");
    BootstrapButton badges = new BootstrapButton("Badges");
    BootstrapButton users = new BootstrapButton("Users");

    BootstrapNavLink userName = new BootstrapNavLink();
    BootstrapNavLink logInOut = new BootstrapNavLink();

    public StaticView(VerticalLayout body) {
        setWidth("100%");

        navbar.setWidth("100%");
        navbar.setPosition(BootstrapNavbar.Position.TOP);

        header.setHeight("100px");
        header.setWidth(HelloUI.SIZE, Unit.PIXELS);

        body.setWidth(HelloUI.SIZE, Unit.PIXELS);

        footer.setHeight("100px");
        footer.setWidth(HelloUI.SIZE, Unit.PIXELS);

        if (HelloUI.getCurrent().getSession().getAttribute("isLoggedIn").equals(false)) {
            logInOut.setText("login | signup");
        } else if (HelloUI.getCurrent().getSession().getAttribute("isLoggedIn").equals(true)) {
            logInOut.setText("logout");
            userName.setText(((HelloUI) HelloUI.getCurrent()).getUser().getUserName());
        }

        initNavbar();
        initHeader();
        initFooter();

        addComponents(
                navbar,
                header,
                body,
                footer
        );
        setComponentAlignment(header, Alignment.TOP_CENTER);
        setComponentAlignment(body, Alignment.TOP_CENTER);
        setComponentAlignment(footer, Alignment.BOTTOM_CENTER);
        setExpandRatio(body, 1);
    }

    private void initNavbar() {
        navbar.setInverse(true);

        BootstrapBrand ramBadger = new BootstrapBrand("RamBadger");
        BootstrapNav login_help_search_NAV = new BootstrapNav();
        BootstrapDropdown help = new BootstrapDropdown("help");
        BootstrapNavSearch form = new BootstrapNavSearch("Search", 10);

        ramBadger.setHref("http://localhost:8079/#!");

        login_help_search_NAV.addNav(userName);
        login_help_search_NAV.addNav(logInOut);
        login_help_search_NAV.addNav(new BootstrapDivider());
        login_help_search_NAV.addNav(help);
        help.setText("help");
        help.addNav(new BootstrapNavHeader("HELP HEADER"));
        help.addNav(new BootstrapNavLink("HELP 1"));
        help.addNav(new BootstrapNavLink("HELP 2"));
        help.addNav(new BootstrapNavLink("HELP 3"));
        help.setRightDropdown(true);

        if (HelloUI.getCurrent().getSession().getAttribute("isLoggedIn").equals(false)) {
            logInOut.setHref("http://localhost:8079/#!login");
        } else {
            logInOut.setHref("http://localhost:8079/#!logout");
            userName.setHref("http://localhost:8079/#!users/" +
                    ((HelloUI) HelloUI.getCurrent()).getUser().getUserName());
        }

        navbar.addNav(ramBadger);
        navbar.addNav(form);
        navbar.addNav(login_help_search_NAV);

        form.setSize(3);

        form.setAlignment(BootstrapNav.Alignment.RIGHT);
        login_help_search_NAV.setAlignment(BootstrapNav.Alignment.RIGHT);
    }

    private void initHeader() {
        Embedded vcu = new Embedded();

        float mag = 2.5f;
        vcu.setHeight(31 * mag, Unit.PIXELS);
        vcu.setWidth(72 * mag, Unit.PIXELS);
        int buttonSize = 110;
        mainButtonGroup.setWidth(buttonSize * 5, Unit.PIXELS);
        organizations.setWidth(buttonSize, Unit.PIXELS);
        tracks.setWidth(buttonSize, Unit.PIXELS);
        submissions.setWidth(buttonSize, Unit.PIXELS);
        badges.setWidth(buttonSize, Unit.PIXELS);
        users.setWidth(buttonSize, Unit.PIXELS);

        organizations.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(Organizations.NAME);
            }
        });
        tracks.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(Tracks.NAME);
            }
        });
        submissions.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(Submissions.NAME);
            }
        });
        badges.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(Badges.NAME);
            }
        });
        users.addListener(new Listener() {
            @Override
            public void componentEvent(Event event) {
                getUI().getNavigator().navigateTo(Users.NAME);
            }
        });

        vcu.setSource(new FileResource(new File(HelloUI.resourceLocation + "/WEB-INF/classes/New_VCU_Rams_Logo.png")));
        mainButtonGroup.addButton(organizations);
        mainButtonGroup.addButton(tracks);
        mainButtonGroup.addButton(submissions);
        mainButtonGroup.addButton(badges);
        mainButtonGroup.addButton(users);

        HSpacer spacer = new HSpacer((int) (72 * mag));
        header.addComponents(vcu, mainButtonGroup, spacer);
        header.setComponentAlignment(vcu, Alignment.TOP_CENTER);
        header.setComponentAlignment(mainButtonGroup, Alignment.MIDDLE_CENTER);
        header.setExpandRatio(mainButtonGroup, 1);
    }

    private void initFooter() {
    }

}